//
//  MSSearchTableViewController.m
//  SocietySale
//
//  Created by Mike on 16/11/9.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSSearchTableViewController.h"
#import "MSHomeCell.h"
#import "MSTradeCell.h"
#import "MSSearchHistoryCell.h"

@interface MSSearchTableViewController ()

@end

@implementation MSSearchTableViewController

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.searchType == MSSearchType_Project) {//项目
        static NSString *identifier = @"customerCell";
        MSHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell==nil) {
            cell = [[MSHomeCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
            cell.introductionLabel.hidden = YES;
//            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.model = self.dataArray[indexPath.row];
        return cell;
    }
    else if(self.searchType == MSSearchType_Trade){//交易
        static NSString *identifier = @"EstateCell";
        MSTradeCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell==nil) {
            cell = [[MSTradeCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
//            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.model = self.dataArray[indexPath.row];
        return cell;
    }
    else{//搜索记录cell
        static NSString *identifier = @"CommanCell";
        MSSearchHistoryCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell==nil) {
            cell = [[MSSearchHistoryCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
//            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.delegate = (id)self.delegate;
        cell.index = indexPath.row;
        if (indexPath.row == self.dataArray.count-1) {
            cell.horizonLineView.frame = CGRectMake(0, 49.4, SCREEN_WIDTH, 0.6);
        }
        else{
            cell.horizonLineView.frame = CGRectMake(12, 49.4, SCREEN_WIDTH-24, 0.6);
        }
        cell.textLabel.text = self.dataArray[indexPath.row];
        cell.textLabel.textColor = kLightBlackColor;
        cell.textLabel.font = HeitiFont(15);
        return cell;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.searchType == MSSearchType_Project) {//项目
        return 75;//[MSHomeCell cellHeightWithModel:self.dataArray[indexPath.row]];
    }
    else if(self.searchType == MSSearchType_Trade){//交易
        return [MSTradeCell cellHeightWithModel:self.dataArray[indexPath.row]];
    }
    return 50;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (self.searchType == MSSearchType_History) {
        return 50;
    }
    return 0.01;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (self.searchType == MSSearchType_History) {
        UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 50)];
        bgView.backgroundColor = kDefaultViewBackgroundColor;
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, 150, 50)];
        titleLabel.text = @"历史搜索";
        titleLabel.textColor = kDarkGrayColor;
        titleLabel.font = HeitiFont(14);
        [bgView addSubview:titleLabel];
        
        GXButton *clearBtn = [GXButton buttonWithType:UIButtonTypeCustom];
        clearBtn.frame = CGRectMake(SCREEN_WIDTH-110, 0, 100, 50);
        clearBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [clearBtn setTitleColor:kLightBlackColor forState:UIControlStateNormal];
        [clearBtn setBackgroundImage:[UIImage imageNamed:@"dot"] forState:UIControlStateHighlighted];
        [clearBtn addTarget:self action:@selector(clearClick:) forControlEvents:UIControlEventTouchUpInside];
        [bgView addSubview:clearBtn];
        if (self.dataArray.count) {
            [clearBtn setTitle:@"清除搜索历史" forState:UIControlStateNormal];
            [clearBtn setImage:[UIImage imageNamed:@"clear_history"] forState:UIControlStateNormal];
        }
        else{
            [clearBtn setTitle:@"无搜索记录" forState:UIControlStateNormal];
            clearBtn.enabled = NO;
        }

        return bgView;
    }
    return nil;
}

- (void)clearClick:(GXButton*)clickBtn{
    if (self.delegate && [self.delegate respondsToSelector:@selector(clearClick:)]) {
        [self.delegate performSelector:@selector(clearClick:) withObject:nil];
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    if ([self.delegate respondsToSelector:@selector(scrollViewWillBeginDragging:)]) {
        [self.delegate performSelector:@selector(scrollViewWillBeginDragging:) withObject:self];
    }
}

@end
